1786D - Letter Exchange - CodeForces Solution


brute force constructive algorithms graphs implementation

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
 
struct moves
{
    int i1, i2;
    char c1, c2;
};
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
 
    int tt; cin >> tt;
 
    while(tt--)
    {
        int n; cin >> n;
 
        vector<string>a(n);
        for(auto &i : a)cin >> i;
 
        set<int>adj[3][3];
        //adj[maxi][not_present] ;
        vector<moves>answer;
 
        string s = "win";
 
        for(int i = 0; i < n; i++)
        {
            int W = 0, I = 0, N = 0;
 
            for(auto &c : a[i])
            {
                if(c == 'w') W++;
                if(c == 'i') I++;
                if(c == 'n') N++;
            }
 
            if(W >= 2 && !I) adj[0][1].insert(i);
            if(W >= 2 && !N) adj[0][2].insert(i);
            if(I >= 2 && !W) adj[1][0].insert(i);
            if(I >= 2 && !N) adj[1][2].insert(i);
            if(N >= 2 && !W) adj[2][0].insert(i);
            if(N >= 2 && !I) adj[2][1].insert(i);
        }
 
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {     // 0,2==wwi
                  //2,0==nni
                    while(!adj[i][j].empty() && !adj[j][i].empty())
                    {
                        moves A; 
                        A.i1 = *adj[i][j].begin();
                        A.i2 = *adj[j][i].begin();
                        A.c1 = s[i]; A.c2 = s[j];
    
                        adj[i][j].erase(A.i1);
                        adj[j][i].erase(A.i2);
    
                        answer.push_back(A);
                    }
 
                    int x = 3 - (i + j);
                    //wwi ke lie now nni is not present 
                    //wwi ke lie we search now nnw
                    while(!adj[i][j].empty() && !adj[j][x].empty())
                    {
                        moves A; 

                        A.i1 = *adj[i][j].begin();
                        A.i2 = *adj[j][x].begin();
                        A.c1 = s[i]; A.c2 = s[j];
 
                        adj[i][j].erase(A.i1);
                        adj[j][x].erase(A.i2);
                        adj[i][x].insert(A.i2);
 
                        answer.push_back(A);
                    }
            }
        }
 
        cout << answer.size() << "\n";
 
        for(auto &x : answer) cout << x.i1 + 1 << " " << x.c1 << " " << x.i2 + 1 << " " << x.c2 << "\n";
    }
}


Comments

Submit
0 Comments
More Questions

80A - Panoramix's Prediction
1354B - Ternary String
122B - Lucky Substring
266B - Queue at the School
1490A - Dense Array
1650B - DIV + MOD
1549B - Gregor and the Pawn Game
553A - Kyoya and Colored Balls
1364A - XXXXX
1499B - Binary Removals
1569C - Jury Meeting
108A - Palindromic Times
46A - Ball Game
114A - Cifera
776A - A Serial Killer
25B - Phone numbers
1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture